﻿namespace DFGK_WMS.WebAPI.Tools
{
    public class TimerTask:IHostedService,IDisposable
    {
        private System.Threading.Timer? timer;

        public void Dispose()
        {
            timer?.Dispose();
            throw new NotImplementedException();
        }

        //public TimerTask()
        //{
        //    timer = new Timer(DoTask, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
        //}

        public Task StartAsync(CancellationToken cancellationToken)
        {
            
            timer = new System.Threading.Timer(DoTask, null, TimeSpan.Zero, TimeSpan.FromSeconds(30));
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            timer?.Change(Timeout.Infinite, 0);
            throw new NotImplementedException();
        }

        private void DoTask(object state)
        {
            // 定时任务逻辑
            Console.WriteLine(DateTime.Now);
        }
    }
}
